<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>less的用法 | dmf的个人博客</title><meta name="author" content="dmf"><meta name="copyright" content="dmf"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="描述">
<meta property="og:type" content="article">
<meta property="og:title" content="less的用法">
<meta property="og:url" content="https://gitee.com/dmf-c/blog/2021/05/01/css_file/LessUsage/index.html">
<meta property="og:site_name" content="dmf的个人博客">
<meta property="og:description" content="描述">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img1.baidu.com/it/u=1828439677,2166285235&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=300">
<meta property="article:published_time" content="2021-04-30T16:00:00.000Z">
<meta property="article:modified_time" content="2022-10-12T07:51:44.527Z">
<meta property="article:author" content="dmf">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img1.baidu.com/it/u=1828439677,2166285235&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=300"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://gitee.com/dmf-c/blog/2021/05/01/css_file/LessUsage/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'less的用法',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-12 15:51:44'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 找文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw iconfont icon-fenlei1"></i><span> 分类</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw iconfont icon-biaoqian1"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw iconfont icon-shijianzhou"></i><span> 归档</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">dmf的个人博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 找文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw iconfont icon-fenlei1"></i><span> 分类</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw iconfont icon-biaoqian1"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw iconfont icon-shijianzhou"></i><span> 归档</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">less的用法</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2021-04-30T16:00:00.000Z" title="发表于 2021-05-01 00:00:00">2021-05-01</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/css/">css</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="less的用法"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>HTML和CSS属于标记语言，所以很难像JS一样定义变量、编写方法、实现模块化开发等。而目前的CSS编写模式中，都是定义一些公共的样式类名，哪一块的HTML需要这个样式，就去增加对应的样式类名，所以我们经常看到一个标签上存在很多样式类名，在这种模式中我们要时常关注CSS的优先级，避免样式的重叠覆盖…</p>
<p>为了解决CSS的这一困境，CSS预处理预编译的思想脱颖而出，比较具有代表性的有LESS、SASS、Stylus。它们在传统的CSS基础上增加了大量的新的语法，编写方式，常用的函数等，可以让我们的CSS像JS一样成为一门编程语言。本文主要介绍LESS的语法和使用。</p>
<h2 id="LESS的编译"><a href="#LESS的编译" class="headerlink" title="LESS的编译"></a>LESS的编译</h2><p>编写完成的LESS代码是不能直接在浏览器中运行的，需要编译成正常的CSS代码。那么我们首先就来学习一下常用的LESS编译方式。</p>
<h3 id="在浏览器中调用LESS-JS"><a href="#在浏览器中调用LESS-JS" class="headerlink" title="在浏览器中调用LESS.JS"></a>在浏览器中调用LESS.JS</h3><p>LESS只支持在现代浏览器中运行（最新版本的Chrome, Firefox, Safari 和 IE）。我们不建议在生产环境中使用LESS客户端，因为在将LESS编译成CSS的时候，用户会看到加载延迟的现象，即便在浏览器中有不足一秒的加载延迟，但也会降低性能。</p>
<p>首先引入我们设置样式的LESS文件，<strong>注意：这里的rel&#x3D;’stylesheet&#x2F;less’</strong></p>
<pre><code>&lt;link type=&quot;text/css&quot; rel=&quot;stylesheet/less&quot; href=&quot;1.less&quot;/&gt;
</code></pre>
<p>然后引入less.js文件</p>
<pre><code>&lt;script src=&quot;//cdnjs.cloudflare.com/ajax/libs/less.js/3.8.1/less.min.js&quot; &gt;&lt;/script&gt;
</code></pre>
<p><strong>浏览器中调用有个好处:可以开启监视模式,只要我们的LESS改变，在一定时间内，浏览器就会重新的编译，我们可以看到想要的效果。</strong>具体操作如下:</p>
<pre><code>&lt;script charset=&quot;utf-8&quot; type=&quot;text/javascript&quot;&gt;
//-&gt;在引入LESS之前设置一个全局的变量less,配置一些参数值(根据情况自行选择需要配置的项)
  var less = &#123;
    //-&gt;env:设置运行的环境(生产模式还是开发模式)
    //production:编译后的CSS缓存到本地localStorage中
    //development:没有把编译后的CSS缓存到本地,在URL不是标准的格式下(例如:file://...),自动设置为development
    env: &quot;development&quot;,
    //-&gt;poll:在监视模式下，每两次请求之间的时间间隔(ms)
    poll:500
    &#125;
&lt;/script&gt;     
&lt;script src=&quot;//cdnjs.cloudflare.com/ajax/libs/less.js/3.8.1/less.min.js&quot; &gt;&lt;/script&gt;
&lt;script charset=&quot;utf-8&quot; type=&quot;text/javascript&quot;&gt;
  //-&gt;启用监视模式(env必须要设置成development)
  less.watch();
&lt;/script&gt;   
</code></pre>
<p><strong>需要特别注意的是，由于浏览器端使用时是使用ajax来拉取.less文件，因此直接在本机文件系统打开（即地址是file:&#x2F;&#x2F;开头）或者是有跨域的情况下会拉取不到.less文件，导致样式无法生效。</strong>所以一般less做css预处理，很少采用引用less的方式，一般是把less编译成css之后，引用生成的css文件。</p>
<pre><code>// index.html文件
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;less&lt;/title&gt;
    &lt;link type=&quot;text/css&quot; rel=&quot;stylesheet/less&quot; href=&quot;css/styles.less&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;header&gt;
        &lt;p&gt;Hello World!&lt;/p&gt;
    &lt;/header&gt;
&lt;script src=&quot;./less.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;   
&lt;/body&gt;
&lt;/html&gt;

// styles.less文件
@blue:#5B83AD;
@size:50px;
header &#123;
    color: @blue;
    font-size: @size;
&#125;
</code></pre>
<p>如果直接引入less文件，便会出现如下错误：</p>
<p>可以尝试用一个简单的web server启动网站就能解决，譬如lite-server</p>
<pre><code>npm install -g lite-server
lite-server //to run
</code></pre>
<h3 id="使用NODE命令编译LESS-单个文件"><a href="#使用NODE命令编译LESS-单个文件" class="headerlink" title="使用NODE命令编译LESS(单个文件)"></a>使用NODE命令编译LESS(单个文件)</h3><p>这种方式是目前项目中最常用的方式，适用于项目的生产环境，它是把我们的LESS文件编译成CSS文件，我们项目中直接的引入CSS文件即可，<strong>基本步骤：安装-&gt;编译&#x2F;压缩编译-&gt;或者使用NODE代码实现批量编译等</strong></p>
<p>把LESS模块安装到全局NODE环境中</p>
<pre><code>npm install less -g
</code></pre>
<p>使用命令进行编译</p>
<pre><code>//-&gt;把styles.less文件编译成styles.css文件(如果没有这个CSS文件自己会创建)
lessc styles.less styles.css

//-&gt;编译完成的CSS文件是经过压缩的
lessc styles.less styles.min.css -x或者--compress
</code></pre>
<p>经过node命令编译代码前后对比:</p>
<pre><code>// styles.less文件(编译前)
@blue:#5B83AD;
@size:50px;
header &#123;
    color: @blue;
    font-size: @size;
&#125;

// styles.css文件(编译后)
header &#123;
color: #5B83AD;
font-size: 50px;
&#125;
</code></pre>
<h3 id="在NODE环境中编写批量编译的代码"><a href="#在NODE环境中编写批量编译的代码" class="headerlink" title="在NODE环境中编写批量编译的代码"></a>在NODE环境中编写批量编译的代码</h3><p>我们在上述用NODE命令编译的时候，一次只能编译一个文件，这样，如果页面中有多个LESS，每一次编译都是比较耗费时间的，所以我们结合NODE的FS文件读写操作，可以写一套批量编译的代码。</p>
<pre><code>//-&gt;定义编译文件目录和目标导出目录
var dirPath = &quot;./less/&quot;, tarPath = &quot;./css/&quot;;
//-&gt;导入NODE中需要使用的模块
var fs = require(&quot;fs&quot;),
    less = require(&quot;less&quot;);
//-&gt;读取dirPath中的所有文件,检查文件的类型,只有LESS文件我们才进行存储
var ary = [],
    files = fs.readdirSync(dirPath);
files.forEach(function (file, index) &#123;
    /\.(LESS)/i.test(file) ? ary.push(file) : null;
&#125;);
//-&gt;把目录下的所有文件进行编译,把编译完成的结果保存在指定的目录中
ary.forEach(function (file) &#123;
    var newFile = file.replace(&quot;.less&quot;, &quot;.css&quot;),
        conFile = fs.readFileSync(dirPath + file, &quot;utf-8&quot;);
    less.render(conFile, &#123;compress: true&#125;, function (error, output) &#123;
        fs.writeFileSync(tarPath + newFile, output.css, &quot;utf-8&quot;);
    &#125;);
&#125;);
</code></pre>
<h2 id="LESS的语法"><a href="#LESS的语法" class="headerlink" title="LESS的语法"></a>LESS的语法</h2><p>LESS的基础语法基本上分为以下几个方面：变量、混合(Mixins)、嵌套规则、运算、函数、作用域等。</p>
<h3 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h3><p><strong>和JS中的变量一样，只是LESS的变量定义不是使用VAR而是使用@。</strong></p>
<pre><code>//-&gt;LESS代码
  @link-color: #428bca;
  @link-color-hover: darken(@link-color, 10%);
  a &#123;
      color: @link-color;
      &amp;:hover &#123;
          color: @link-color-hover;
      &#125;
  &#125;
//-&gt;编译为CSS的结果
  a &#123;
      color: #428bca;
  &#125;
  a:hover &#123;
      color: #3071a9;
  &#125;
</code></pre>
<p>除了上述用变量存储公用的属性值，我们还可以用变量存储公用的URL、选择器等等</p>
<pre><code>//-&gt;LESS代码
.@&#123;selector&#125; &#123;
    width: 100px;
    height: 100px;
    @&#123;property&#125;: #000;
    background: url(&quot;@&#123;bgImg&#125;/test.png&quot;);
&#125;
@selector: box;
@bgImg: &quot;../img&quot;;
@property: color;
</code></pre>
<h3 id="混合-Mixins"><a href="#混合-Mixins" class="headerlink" title="混合(Mixins)"></a>混合(Mixins)</h3><p><strong>① 基本使用</strong></p>
<p>混合可以将一个定义好的class A轻松的引入到另一个class B中，从而简单实现class B继承class A中的所有属性。我们还可以带参数地调用，就像使用函数一样。</p>
<pre><code>//-&gt;LESS代码
.public &#123;
    width: 100px;
    height: 100px;
&#125;
nav ul &#123;
    .public;
    list-style: none;
&#125;
//-&gt;编译为CSS的结果
.public &#123;
    width: 100px;
    height: 100px;
&#125;
nav ul &#123;
    width: 100px;
    height: 100px;
    list-style: none;
&#125;
</code></pre>
<p>上述的代码，nav ul是把public中设定的样式属性值copy了一份到自己的样式中。如果你想在编译完成的结果中不输出public这个样式的结果，只需要按照下述的代码编写即可：</p>
<pre><code>//-&gt;LESS代码
.public() &#123;//-&gt;在选择器后面加上()就可以不编译这个样式了
    width: 100px;
    height: 100px;
&#125;
nav ul &#123;
    .public;//如果public有子孙元素的样式，同样也会被复制过来
    list-style: none;
&#125;
//-&gt;编译为CSS的结果
nav ul &#123;
    width: 100px;
    height: 100px;
    list-style: none;
&#125;
</code></pre>
<p><strong>② extend</strong></p>
<p>虽然在上述的案例中，nav ul把public中的样式继承了过来，但是原理却是把代码copy一份过来，这样编译后的CSS中依然会存留大量的冗余CSS代码，为了避免这一点，我们可以使用extend伪类来实现样式的继承使用。<strong>和原来的选择器共用一套样式，但要保证原来的选择器不加括号。</strong></p>
<pre><code>//-&gt;LESS代码
.public &#123;
    width: 100px;
    height: 100px;
&#125;
nav ul &#123;
    &amp;:extend(.public);
    list-style: none;
&#125;
//-&gt;编译为CSS的结果
.public, nav ul &#123;
    width: 100px;
    height: 100px;
&#125;
nav ul &#123;
    list-style: none;
&#125;
</code></pre>
<h3 id="嵌套规则"><a href="#嵌套规则" class="headerlink" title="嵌套规则"></a>嵌套规则</h3><p>我们可以在一个选择器中嵌套另一个选择器来实现继承，这样很大程度减少了代码量，并且代码看起来更加的清晰。</p>
<pre><code>//-&gt;LESS代码
#header &#123;
color: black;
.navigation &#123;
    font-size: 12px;
&#125;
.logo &#123;
    width: 300px;
    &amp;:hover &#123; text-decoration: none &#125;
&#125;
&#125;

//-&gt;编译为CSS的结果
#header &#123; color: black; &#125;
#header .navigation &#123;
font-size: 12px;
&#125;
#header .logo &#123; 
width: 300px; 
&#125;
#header .logo:hover &#123;
text-decoration: none;
&#125;
</code></pre>
<h3 id="函数-amp-运算"><a href="#函数-amp-运算" class="headerlink" title="函数 &amp; 运算"></a>函数 &amp; 运算</h3><p>运算提供了加，减，乘，除操作；我们可以做属性值和颜色的运算，这样就可以实现属性值之间的复杂关系。LESS中的函数一一映射了JavaScript代码，如果你愿意的话可以操作属性值。</p>
<p>任何数字、颜色或者变量都可以参与运算. 来看一组例子:</p>
<pre><code>@base: 5%;
@filler: @base * 2;
@other: @base + @filler;
color: #888 / 4;
background-color: @base-color + #111;
height: 100% / 2 + @filler;
</code></pre>
<p>LESS 提供了一系列的颜色运算函数. 颜色会先被转化成 HSL 色彩空间, 然后在通道级别操作:</p>
<pre><code>lighten(@color, 10%);     // return a color which is 10% *lighter* than @color
darken(@color, 10%);      // return a color which is 10% *darker* than @color
</code></pre>
<p>LESS提供了一组方便的数学函数，你可以使用它们处理一些数字类型的值:</p>
<pre><code>round(1.67); // returns `2`
ceil(2.4);   // returns `3`
floor(2.6);  // returns `2`
</code></pre>
<h3 id="命名空间和作用域"><a href="#命名空间和作用域" class="headerlink" title="命名空间和作用域"></a>命名空间和作用域</h3><p>LESS 中的作用域跟其他编程语言非常类似，首先会从本地查找变量或者混合模块，如果没找到的话会去父级作用域中查找，直到找到为止。</p>
<pre><code>@var: red;
#page &#123;
@var: white;
#header &#123;
    color: @var; // white
&#125;
&#125;
#footer &#123;
color: @var; // red  
&#125;
</code></pre>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/dmf-c/blog">dmf</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/dmf-c/blog/2021/05/01/css_file/LessUsage/">https://gitee.com/dmf-c/blog/2021/05/01/css_file/LessUsage/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/dmf-c/blog" target="_blank">dmf的个人博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2021/05/15/css_file/CommonLayouts/"><img class="prev-cover" src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">常见的几种css布局</div></div></a></div><div class="next-post pull-right"><a href="/blog/2021/04/17/js_file/ContextStack/"><img class="next-cover" src="https://img1.baidu.com/it/u=3691791879,443411760&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=889" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JavaScript执行上下文和执行栈</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">dmf</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">业精于勤，荒于嬉。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#LESS%E7%9A%84%E7%BC%96%E8%AF%91"><span class="toc-number">1.</span> <span class="toc-text">LESS的编译</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%AD%E8%B0%83%E7%94%A8LESS-JS"><span class="toc-number">1.1.</span> <span class="toc-text">在浏览器中调用LESS.JS</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8NODE%E5%91%BD%E4%BB%A4%E7%BC%96%E8%AF%91LESS-%E5%8D%95%E4%B8%AA%E6%96%87%E4%BB%B6"><span class="toc-number">1.2.</span> <span class="toc-text">使用NODE命令编译LESS(单个文件)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8NODE%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%BC%96%E5%86%99%E6%89%B9%E9%87%8F%E7%BC%96%E8%AF%91%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="toc-number">1.3.</span> <span class="toc-text">在NODE环境中编写批量编译的代码</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#LESS%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">LESS的语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F"><span class="toc-number">2.1.</span> <span class="toc-text">变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%B7%E5%90%88-Mixins"><span class="toc-number">2.2.</span> <span class="toc-text">混合(Mixins)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B5%8C%E5%A5%97%E8%A7%84%E5%88%99"><span class="toc-number">2.3.</span> <span class="toc-text">嵌套规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0-amp-%E8%BF%90%E7%AE%97"><span class="toc-number">2.4.</span> <span class="toc-text">函数 &amp; 运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%E5%92%8C%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">2.5.</span> <span class="toc-text">命名空间和作用域</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2023/01/08/vue_file/vuenote/" title="vue3入门笔记"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue3入门笔记"/></a><div class="content"><a class="title" href="/blog/2023/01/08/vue_file/vuenote/" title="vue3入门笔记">vue3入门笔记</a><time datetime="2023-01-07T16:00:00.000Z" title="发表于 2023-01-08 00:00:00">2023-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/02/20/vue_file/comm/" title="vue组件间通信六种方式"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue组件间通信六种方式"/></a><div class="content"><a class="title" href="/blog/2022/02/20/vue_file/comm/" title="vue组件间通信六种方式">vue组件间通信六种方式</a><time datetime="2022-02-19T16:00:00.000Z" title="发表于 2022-02-20 00:00:00">2022-02-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/02/05/wxEcharts/" title="微信小程序使用 echarts"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="微信小程序使用 echarts"/></a><div class="content"><a class="title" href="/blog/2022/02/05/wxEcharts/" title="微信小程序使用 echarts">微信小程序使用 echarts</a><time datetime="2022-02-04T16:00:00.000Z" title="发表于 2022-02-05 00:00:00">2022-02-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/01/30/vue_file/vue_router/" title="vue-router学习"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue-router学习"/></a><div class="content"><a class="title" href="/blog/2022/01/30/vue_file/vue_router/" title="vue-router学习">vue-router学习</a><time datetime="2022-01-29T16:00:00.000Z" title="发表于 2022-01-30 00:00:00">2022-01-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/01/16/vue_file/vuex/" title="Vuex使用方法"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Vuex使用方法"/></a><div class="content"><a class="title" href="/blog/2022/01/16/vue_file/vuex/" title="Vuex使用方法">Vuex使用方法</a><time datetime="2022-01-15T16:00:00.000Z" title="发表于 2022-01-16 00:00:00">2022-01-16</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By dmf</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>